<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    function Food(name, price) {
      this.name = name
      this.price = price
      this.ingredients = []
    }

    // 添加原料
    Food.prototype.addIngredient = function (name, weight) {
      this.ingredients.push({ name: name, weight: weight })
    }

    // 删除原料
    Food.prototype.removeIngredient = function (name) {
     this.ingredients.findIndex((item) => item.name === this.name)
     return this.ingredients.splice(0,1)
    }

    // 重量总和
    Food.prototype.calculateTotalWeight = function () {
    return this.ingredients.reduce((prev,item)=> prev + item.weight,0)
    } 

    Food.prototype.toString = function () {
    const arr= this.ingredients.map(item=> item.name)
    return`名称:${this.name} 价格:${this.price} 原料:${arr.join('')}`
    }
    const food = new Food('面包', 5)
    food.addIngredient('蓝鳍金枪鱼', 10)
    food.addIngredient('蓝电霸王龙', 20)
    food.addIngredient('蓝银皇头骨', 30)
    food.addIngredient('肉', 40) 
    console.log(food.removeIngredient());
    console.log(food.calculateTotalWeight());
    console.log(food.toString());
  
  </script>
</html>
